<template>
  <div>

      <div style="margin-bottom:15px">
        <el-input v-model="params.name" style="width:200px" placeholder="请输入公告标题"></el-input>
        <el-button type="warning" style="margin-left:10px" @click="findBySearch()">查询</el-button>
        <el-button type="warning" style="margin-left:10px" @click="reset()">清空</el-button>
        <el-button type="primary" style="margin-left:10px" @click="add()">新增</el-button>
      </div>
      <div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="name" label="公告标题" ></el-table-column>
          <el-table-column prop="content" label="公告内容" ></el-table-column>
          <el-table-column prop="time" label="更新时间"></el-table-column>
          
          <el-table-column  label="操作">
            <template slot-scope="scope">
              <el-button type="primary" @click="edit(scope.row)">编辑</el-button>
              <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)">
                <el-button slot="reference" type="danger" style="margin-left:5px">删除</el-button>
              </el-popconfirm>
              <!-- <el-button type="danger">删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>
      </div>

            <div style="margin-top: 10px;">
          
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="params.pageNum"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="params.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>

        <!-- 对话框 -->
  <div>

        <el-dialog title="请填写信息" :visible.sync="dialogFormVisible" width="30%">
            <el-form :model="form">
              <el-form-item label="公告标题" label-width="15%">
                <el-input v-model="form.name" autocomplete="off" style="width:90%"></el-input>
              </el-form-item>


              <el-form-item label="公告内容" label-width="15%">
                <el-input v-model="form.content" type="textarea" autocomplete="off" style="width:90%"></el-input>
              </el-form-item>

              
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="submit()">确 定</el-button>
            </div>
          </el-dialog>

        </div>

  </div>
</template>

<script>
import request from '@/utils/request'
export default {
  data() {
    return {
      params:{
        name:'',
        pageNum:1,
        pageSize:5
      },
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form:{}
    }
  },
  //页面加载的时候，做一些事情，在created里面
  created(){
      this.findBySearch()
  },
  //定义一些页面上空间触发的事件调用的方法
  methods:{
    
    findBySearch(){
      request.get("/notice/search",{
        params:this.params
      }).then(res =>{
        if(res.code === '0'){
          this.tableData = res.data.list
          this.total = res.data.total
        }else{

        }
      })
    },
    add(){
      this.form = {}
      this.dialogFormVisible = true
    },
    edit(obj){
      this.dialogFormVisible = true
      this.form = obj

    },
    reset(){
        this.params={
        pageNum:1,
        pageSize:5,
        name:'',
        phone:''
        }
        this.findBySearch()
    },
    handleSizeChange(pageSize){
        this.params.pageSize = pageSize
        this.findBySearch()
    },
    handleCurrentChange(pageNum){
      this.params.pageNum = pageNum
        this.findBySearch()
    },
    submit(){
      request.post("/notice",this.form).then(res =>{
        if(res.code === '0'){
            this.$message.success("操作成功")
            this.dialogFormVisible = false
            this.findBySearch()
        }else{
            this.$message.error(res.msg)
        }
      })
    },
      del(id){
            request.delete("/notice/"+id).then(res =>{
              if(res.code === '0'){
                this.$message.success("删除成功")
                this.findBySearch()
              }else{
                this.$message.error(res.msg)
                }
            })
      }
      
    }    // methods

  
}   //export default 
  </script>